//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 鹏 周 on 15/12/16.
//  Copyright © 2015年 ycit. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"
private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {
    init() {
        let layout = UICollectionViewFlowLayout()
        //设置item的大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //横向 滚动
        layout.scrollDirection = .Horizontal
        //设置item的上下左右的间隔
        //这个是设置两个item的之间没有黑边
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        super.init(collectionViewLayout: layout)
        //取消弹簧效果  弹簧效果会看到最右边的黑色的地方
         collectionView?.bounces = false
        //取消滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        //不设置分页 会滚动的特别快
        collectionView?.pagingEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()


        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

 
    }

   
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
      
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)!
        
        if index.item == imageCount - 1 {
            cell.showAnimation()
        }

    }
}


//创建类 UICollectionViewCell的子类
class NewFeatureCell: UICollectionViewCell {
    var index: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(index+1)")
            
            startBtn.hidden = true
          
        }
        
    }
    
    private func showAnimation() {
    startBtn.transform = CGAffineTransformMakeScale(0, 0)
    startBtn.hidden = false

    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
        self.startBtn.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            print("动画完成OK")
        }
        
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
  @objc private func startBtnDidClick() {
    
    print(__FUNCTION__)
    
    }
    
    //设置界面
    private func setupUI() {
        
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)

        //使用SnapKit添加约束
        //snp_makeConstraints 表示添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    //懒加载 加载图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //懒加载开始按钮
    private lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        return btn
        
    }()
}
